package usbcali.edu.co.pqrs.dataaccess.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import usbcali.edu.co.pqrs.dataaccess.api.HibernateDaoImpl;
import usbcali.edu.co.pqrs.modelo.UsuariosInternos;
import usbcali.edu.co.pqrs.modelo.dto.UsuariosInternosDTO;

/**
 * A data access object (DAO) providing persistence and search support for
 * UsuariosInternos entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see lidis.UsuariosInternos
 */
@Scope("singleton")
@Repository("UsuariosInternosDAO")
public class UsuariosInternosDAO extends
		HibernateDaoImpl<UsuariosInternos, Long> implements
		IUsuariosInternosDAO {
	private static final Logger log = LoggerFactory
			.getLogger(UsuariosInternosDAO.class);
	@Resource
	private SessionFactory sessionFactory;

	public static IUsuariosInternosDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IUsuariosInternosDAO) ctx.getBean("UsuariosInternosDAO");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UsuariosInternosDTO> validarLogin(String login,
			String contrasena) throws Exception {

		List<UsuariosInternosDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("validar_login");
			query.setParameter("pLogin", login);
			query.setParameter("pContrasena", contrasena);

			query.setResultTransformer(Transformers
					.aliasToBean(UsuariosInternosDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}

		return listDto;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<UsuariosInternosDTO> consultarRol(String login) throws Exception {

		List<UsuariosInternosDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("consultar_rol");
			query.setParameter("pLogin", login);

			query.setResultTransformer(Transformers.aliasToBean(UsuariosInternosDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			System.out.println("error "+e);
			throw e;
		}

		return listDto;
	}
}
